
#import "Dialog.h"
#import <unistd.h>

@implementation Dialog

static Dialog *instance = nil;

+ (Dialog *)Instance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];

    });
    return instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [super allocWithZone:zone];
    });
    return instance;
}

+ (void)toast:(UIViewController *)controller withMessage:(NSString *)message {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:controller.view animated:YES];
    hud.userInteractionEnabled = NO;
	hud.mode = MBProgressHUDModeText;
    hud.label.text = message;
    hud.bezelView.layer.cornerRadius = 5;
    hud.margin = 10.f;
    [hud setOffset:CGPointMake(hud.offset.x, 150)];
    hud.animationType = MBProgressHUDAnimationZoom;
	hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:2];
}

+ (void)toast:(NSString *)message withPosition:(float)position
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[Dialog getTopWindow] animated:YES];
	hud.mode = MBProgressHUDModeText;
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.label.text = message;
    hud.bezelView.layer.cornerRadius = 5;
	hud.margin = 10.f;
    [hud setOffset:CGPointMake(hud.offset.x, position)];
	hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1];
}

+ (void)toast:(NSString *)message withPosition:(float)position delay:(float)time
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[Dialog getTopWindow] animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.label.text = message;
    hud.bezelView.layer.cornerRadius = 5;
    hud.margin = 10.f;
    [hud setOffset:CGPointMake(hud.offset.x, position)];
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:time];
}

+ (UIWindow *)keyWindow {
    UIWindow *foundWindow = nil;
    NSArray  *windows = [[UIApplication sharedApplication] windows];
    for (UIWindow  *window in windows) {
        if (window.isKeyWindow) {
            foundWindow = window;
            break;
        }
    }
    return foundWindow;
}

+ (UIView *)getTopWindow {
    // 可能出现hub不出现问题，使用keywindow 被键盘遮挡
    UIView *warp = [Dialog keyWindow];
    
    NSArray<__kindof UIWindow *> *windows = [UIApplication sharedApplication].windows;
    // 所以如下处理
    for(UIView *window in windows)
    {
        if([window isKindOfClass:NSClassFromString(@"UIRemoteKeyboardWindow")])
        {
            warp = [UIApplication sharedApplication].windows.lastObject;
        }
    }
    return warp;
}

+ (void)toast:(NSString *)message delay:(float)time {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[Dialog getTopWindow] animated:YES];
    hud.userInteractionEnabled = NO;
    
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    hud.bezelView.layer.cornerRadius = 5;
    hud.contentColor = [UIColor whiteColor];
 
	hud.mode = MBProgressHUDModeText;
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.label.text = message;
    [hud setOffset:CGPointMake(hud.offset.x, 150)];
	hud.margin = 10.f;
	hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:time];
}

+ (void)toastBottom:(NSString *)message {
    [Dialog toast:message delay:2.0];
}

//+ (void)simpleToast:(NSString *)message
//{
//    [SVProgressHUD showOnlyStatus:message withDuration:2.3];
//}
//
//+ (void)hideSimpleToast
//{
//    [SVProgressHUD dismissAfterDelay:2];
//}

+ (void)toastCenter:(NSString *)message {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[Dialog getTopWindow] animated:YES];
	hud.mode = MBProgressHUDModeText;
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.label.text = message;
	hud.margin = 10.f;
    [hud setOffset:CGPointMake(hud.offset.x, -20.0f)];
	hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:2];
}

+ (void)custoastCenter:(NSString *)message {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[Dialog getTopWindow] animated:YES];
	hud.mode = MBProgressHUDModeCustomView;
    hud.userInteractionEnabled = NO;;
    hud.animationType = MBProgressHUDAnimationZoomOut;

    UILabel *notic = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 90)];
    notic.text = message;
    notic.textAlignment = NSTextAlignmentCenter;
    notic.font = [UIFont systemFontOfSize:15];
    notic.textColor = [UIColor blackColor];
    notic.numberOfLines = 0;
    hud.customView = notic;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:2];
}

+ (void)custoastCenter:(NSString *)message delay:(float)time {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[Dialog getTopWindow] animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.userInteractionEnabled = NO;;
    hud.animationType = MBProgressHUDAnimationZoomOut;

    UILabel *notic = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 90)];
    notic.text = message;
    notic.textAlignment = NSTextAlignmentCenter;
    notic.font = [UIFont systemFontOfSize:15];
    notic.textColor = [UIColor blackColor];
    notic.numberOfLines = 0;
    hud.customView = notic;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay: time];
}

+ (void)progressToast:(NSString *)message
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[Dialog getTopWindow] animated:YES];
	hud.mode = MBProgressHUDModeIndeterminate;
    hud.label.text = message;
	hud.margin = 10.f;
    [hud setOffset:CGPointMake(hud.offset.x, -20.0f)];
	hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1];
}

+ (void)showIntView:(UIView *)view progressToast:(NSString *)message {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.label.text = message;
    hud.margin = 10.f;
    [hud setOffset:CGPointMake(hud.offset.x, -20.0f)];
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:1];
}

- (void)gradient:(UIViewController *)controller seletor:(SEL)method {
    HUD = [[MBProgressHUD alloc] initWithView:controller.view];
	[controller.view addSubview:HUD];
//	HUD.dimBackground = YES;
	HUD.delegate = self;
//	[HUD showWhileExecuting:method onTarget:controller withObject:nil animated:YES];
    
 

}

- (void)showProgress:(UIViewController *)controller {
    HUD = [[MBProgressHUD alloc] initWithView:controller.view];
    [controller.view addSubview:HUD];
//    HUD.dimBackground = YES;
    HUD.delegate = self;
    [HUD showAnimated:YES];
}

- (void)showProgress:(UIViewController *)controller withLabel:(NSString *)labelText {
    HUD = [[MBProgressHUD alloc] initWithView:controller.view];
    [controller.view addSubview:HUD];
    HUD.delegate = self;
//    HUD.dimBackground = YES;
    HUD.label.text = labelText;
    [HUD showAnimated:YES];
}

#pragma mark - 显示带说明的菊花，点击hide
///显示带说明的菊花，点击hide
- (void)showProgressTapHide:(UIViewController *)controller withLabel:(NSString *)labelText tag:(NSInteger)tag {
    HUD = [[MBProgressHUD alloc] initWithView:controller.view];
    HUD.tag = tag;
    [controller.view addSubview:HUD];
    HUD.delegate = self;
    //    HUD.dimBackground = YES;
    HUD.label.text = labelText;
    HUD.bezelView.layer.cornerRadius = 14;
    [HUD showAnimated:YES];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideHUD)];
//    [HUD addGestureRecognizer:tap];
}

- (void)wo_hideHUD:(UIViewController *)controller WithTag:(NSInteger)tag {
    
    MBProgressHUD *hud = (MBProgressHUD *)[controller.view viewWithTag:tag];
    [hud hideAnimated:YES];
}

- (void)showCenterProgressWithLabel:(NSString *)labelText
{
    HUD = [[MBProgressHUD alloc] initWithView:[Dialog getTopWindow]];
    [[Dialog getTopWindow] addSubview:HUD];
    HUD.delegate = self;
    //    HUD.dimBackground = YES;
    HUD.label.text = labelText;
    [HUD showAnimated:YES];
}

- (void)hideHUD {
    [HUD hideAnimated:YES];
}

- (void)progressWithLabel:(UIViewController *)controller seletor:(SEL)method {
    HUD = [[MBProgressHUD alloc] initWithView:controller.view];
    [controller.view addSubview:HUD];
    HUD.delegate = self;
    //HUD.labelText = @"数据加载中...";
//    [HUD showWhileExecuting:method onTarget:controller withObject:nil animated:YES];
}

#pragma mark -
#pragma mark Execution code

- (void)myTask {
	sleep(3);
}

- (void)myProgressTask {
	float progress = 0.0f;
	while (progress < 1.0f) {
		progress += 0.01f;
		HUD.progress = progress;
		usleep(50000);
	}
}

- (void)myMixedTask {
	sleep(2);
	HUD.mode = MBProgressHUDModeDeterminate;
	HUD.label.text = @"Progress";
	float progress = 0.0f;
	while (progress < 1.0f)
	{
		progress += 0.01f;
		HUD.progress = progress;
		usleep(50000);
	}
	HUD.mode = MBProgressHUDModeIndeterminate;
	HUD.label.text = @"Cleaning up";
	sleep(2);
	HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark"]] ;
	HUD.mode = MBProgressHUDModeCustomView;
	HUD.label.text = @"Completed";
	sleep(2);
}

#pragma mark -
#pragma mark NSURLConnectionDelegete

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
	expectedLength = [response expectedContentLength];
	currentLength = 0;
	HUD.mode = MBProgressHUDModeDeterminate;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	currentLength += [data length];
	HUD.progress = currentLength / (float)expectedLength;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark"]];
	HUD.mode = MBProgressHUDModeCustomView;
    [HUD hideAnimated:YES afterDelay:1];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [HUD hideAnimated:YES afterDelay:1];
}

#pragma mark -
#pragma mark MBProgressHUDDelegate methods

- (void)hudWasHidden:(MBProgressHUD *)hud {
	[HUD removeFromSuperview];
	HUD = nil;
}

@end
